<?php /** @noinspection PhpUnused */

namespace app\controller;

use app\BaseController;
use app\Request;
use app\Result;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use think\response\Json;

class commodity extends BaseController
{

    /**
     * @throws DbException
     */
    public function getList(Request $request): Json
    {
        $keywords = $request->post("keywords");
        $pageSize = $request->post("pageSize");
        $pageNum = $request->post("pageNum");
        $state = $request->post("state");


        try {
            $list = Db::table("commodity")->where("commodity_name", "like", "%" . $keywords . "%")->where("state",$state)->
            paginate([
                'list_rows' => $pageSize,
                'page' => $pageNum,
            ]);
            return Result::success(
                $list
            );
        } catch (DataNotFoundException $e) {
            return Result::error(10001, "列表获取失败");
        }
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function addCommodity(Request $request): Json
    {
        $commodity_name = $request->post("commodity_name");
        $unit = $request->post("unit");
        $price = $request->post("price");
        $in_num = $request->post("in_num");
        $in_price = $request->post("in_price");
        $payment = $request->post("payment");
        $dealer = $request->post("dealer");



        if (!$commodity_name) {
            return Result::error(10001, "请输入商品名");
        }
        $commodity = Db::table("commodity")->where("commodity_name", $commodity_name)->find();
        if ($commodity) {
            return Result::error(10001, "该商品已存在");
        }

        Db::query("insert into commodity (commodity_name,unit,price,num,date,state ) values (?,?,?,?,Now(),1)", [
            $commodity_name, $unit, $price, $in_num
        ]);
        $commodity = Db::table("commodity")->where("commodity_name", $commodity_name)->find();

        Db::query("insert Into in_log (commodity_id, in_num, in_price, time,payment,dealer) values (?,?,?,now(),?,?)", [
            $commodity['commodity_id'], $in_num, $in_price,$payment,$dealer
        ]);
        return Result::success($commodity, "添加成功！");
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function getDetail($id): Json
    {
        $data = Db::name("commodity")->where("commodity_id", $id)->find();
        return Result::success($data);
    }

    /**
     * @throws DbException
     */
    public function del($id): Json
    {
        Db::name("commodity")->where("commodity_id", $id)->delete();
        Db::name("in_log")->where("commodity_id", $id)->delete();
        Db::name("out_log")->where("commodity_id", $id)->delete();
        return Result::success(null);
    }

    /**
     * @throws DbException
     */
    public function update(Request $request): Json
    {

        $key = $request->post("key");
        $val = $request->post("value");
        $commodity_id = $request->post("commodity_id");

        Db::name("commodity")->where("commodity_id",$commodity_id)->update(
            [$key=>$val]
        );

        return Result::success(null, "修改成功");

    }

}